package com.encryption.logic.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.encryption.model.File;
import com.encryption.model.FilePermissionAsso;
import com.encryption.model.FilePermissionConf;
import com.encryption.model.User;
import com.encryption.service.qrc.DocProcessParam;
import com.log4ic.utils.io.FileUtils;

public class FilePermissionParser {
	
	public static final String FILE_PATH = System.getProperty("catalina.home") + "/webapps/ROOT";
	
	public static List<FilePermissionAsso> parse(File file, FilePermissionConf conf){
		List<FilePermissionAsso> assoList = new ArrayList<FilePermissionAsso>();
		if(conf.getType() == 0){
			FilePermissionAsso asso = createFilePermissionAsso(file, conf);
			asso.setAllowType(0);
			assoList.add(asso);
		}else{
			List<Integer> departIdList = conf.getDepartIdList();
			List<Integer> userIdList = conf.getUserIdList();
			if(departIdList != null && departIdList.size() > 0){
				for (int departId : departIdList){
					FilePermissionAsso asso = createFilePermissionAsso(file, conf);
					asso.setAllowType(2);
					asso.setAllowValue(departId);
					assoList.add(asso);
				}
			}
			if(userIdList != null && userIdList.size() > 0){
				for (int userId : userIdList){
					FilePermissionAsso asso = createFilePermissionAsso(file, conf);
					asso.setAllowType(1);
					asso.setAllowValue(userId);
					assoList.add(asso);
				}
			}
		}
		
		return assoList;
	}
	
	public static FilePermissionConf deParse(List<FilePermissionAsso> assoList){
		FilePermissionConf conf = new FilePermissionConf();
		if(assoList == null || assoList.size() == 0)
			return conf;
			
		FilePermissionAsso asso1 = assoList.get(0);
		conf.setCanView(asso1.getAllowView().intValue() == 1);
		conf.setCanDownload(asso1.getAllowDownload().intValue() == 1);
		conf.setCanPrint(asso1.getAllowPrint().intValue() == 1);
		if(asso1.getAllowType().intValue() == 0)
			conf.setType(0);
		else
			conf.setType(2);
		
		List<Integer> userIdList = new ArrayList<Integer>();
		List<Integer> departIdList = new ArrayList<Integer>();
		
		for(FilePermissionAsso asso : assoList){
			if(asso.getAllowType().intValue() == 1){
				userIdList.add(asso.getAllowValue());
			}else if(asso.getAllowType().intValue() == 2){
				departIdList.add(asso.getAllowValue());
			}
		}
		conf.setUserIdList(userIdList);
		conf.setDepartIdList(departIdList);
		
		return conf;
	}
	
	public static DocProcessParam parse(FilePermissionConf conf, File file){
		DocProcessParam param = new DocProcessParam();
		String sourcePath = FILE_PATH + file.getSwfUrl();
		String targetPath = FILE_PATH + file.getEncryptionUrl();
		if(conf.getIsAll()){
			param.setIsAll(true);
			param.setSourceFilePath(file.getFileUrl());
			param.setUploadUserId(file.getUser() + "");
		}else{
			param.setIsAll(false);
			param.setDeptId(intList2StringList(conf.getDepartIdList()));
			param.setUserId(intList2StringList(conf.getUserIdList()));
		}
		param.setFileId(file.getId() + "");
		param.setUploadUserId(file.getUser() + "");
		param.setSourceFilePath(sourcePath);
		param.setTargetFilePath(targetPath);
		
		return param;
	}
	
	public static DocProcessParam parse(User user, File file){
		DocProcessParam param = new DocProcessParam();
		String fileUrl = file.getFileUrl();
		String sourcePath = FILE_PATH + file.getFileUrl();
		String fileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);
		fileName = FileUtils.getFilePrefix(fileName) + System.currentTimeMillis() + "." + FileUtils.getFileSuffix(fileName);
		
		String targetPath = FILE_PATH + "/file/temp/" + fileName;
		param.setFileId(file.getId() + "");
		param.setUploadUserId(user.getId() + "");
		param.setSourceFilePath(sourcePath);
		param.setTargetFilePath(targetPath);
		
		return param;
	}
	
	private static List<String> intList2StringList(List<Integer> intList){
		List<String> strList = new ArrayList<String>();
		for (Integer intItem : intList)
			strList.add(intItem + "");
		
		return strList;
	}
	
	private static FilePermissionAsso createFilePermissionAsso(File file, FilePermissionConf conf){
		int allowView = conf.isCanView() ? 1 : 0;
		int allowPrint = conf.isCanPrint() ? 1 : 0;
		int allowDownload = conf.isCanDownload() ? 1 : 0;
		int fileLevel = file.getFileLevel();
		int fileId = file.getId();
		
		FilePermissionAsso asso = new FilePermissionAsso();
		asso.setAllowView(allowView);
		asso.setAllowPrint(allowPrint);
		asso.setAllowDownload(allowDownload);
		asso.setFile(fileId);
		asso.setFileLevel(fileLevel);
		
		return asso;
	}
}
